package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.common.PageResult;
import org.example.common.ResponseResult;
import org.example.pojo.entity.TbStudent;
import org.example.service.TbStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api("学生管理")
public class StudentController {

    @Autowired
    private TbStudentService tbStudentService;

    @GetMapping("getStudents")
    @ApiOperation("查询学生信息")
    public PageResult<TbStudent> getStudents(@RequestParam("page") @ApiParam("当前页") Long page,
                                             @RequestParam("pageSize") @ApiParam("页数") Long pageSize){
        PageResult<TbStudent> students = tbStudentService.getStudents(page, pageSize);
        return students;
    }
    @GetMapping("getStudentsList")
    @ApiOperation("根据姓名查询")
    public PageResult<TbStudent> getStudentsList(@RequestParam("page") @ApiParam("当前页") Long page,
                                                 @RequestParam("pageSize") @ApiParam("页数") Long pageSize,
                                                 @RequestParam("name") @ApiParam("学生名字") String name){
        PageResult<TbStudent> studentsList = tbStudentService.getStudentsList(page, pageSize, name);
        return studentsList;
    }

    @PutMapping("editStudent")
    @ApiOperation("编辑学生信息")
    public ResponseResult editStudent(@RequestBody @ApiParam("学生信息") TbStudent student){
        tbStudentService.editStudent(student);
        return ResponseResult.ok();
    }

    @PostMapping("addStudent")
    @ApiOperation("新增学生")
    public ResponseResult addStudent(@RequestBody @ApiParam("学生信息") TbStudent student){
        tbStudentService.addStudent(student);
        return ResponseResult.ok();
    }
    @DeleteMapping("deleteStudent/{sid}")
    @ApiOperation("删除学生")
    public ResponseResult deleteStudent(@PathVariable @ApiParam("学生id") Integer sid){
        tbStudentService.deleteStudent(sid);
        return ResponseResult.ok();
    }

    @GetMapping("download")
    @ApiOperation("excel表格下载")
    public void download(HttpServletResponse response) throws IOException {
        tbStudentService.download(response);
    }

}
